//
//  FirstTableView.m
//  B段项目
//
//  Created by dllo on 15/9/24.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "FirstTableView.h"
#import "PrefixHeader.pch"
#import "FirstCell.h"
#import "FirstModel.h"
#import "AFNetWorkingW.h"
#import <MJRefresh.h>

@interface FirstTableView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableDictionary *mDic;
@property (nonatomic, strong) NSMutableArray *mArr;
@property (nonatomic, strong) FirstModel *firstModel;
@property (nonatomic, strong) UIActivityIndicatorView *activity;

@end

@implementation FirstTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        /* 设置UITableView */
//        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        //        self.bounces = NO;
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[FirstCell class] forCellReuseIdentifier:@"reuse"];
        
        self.mArr = [NSMutableArray array];
        [self setExtraCellLineHidden:self];
        
        self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        //        self.activity.center = self.center;
        self.activity.frame = CGRectMake((ScreenWidth - 80) / 2, (ScreenHeight - 120) / 2, 80, 80);
        self.activity.color = RGB(22, 188, 180);
        [self addSubview:self.activity];
        [self.activity startAnimating];
        
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.firstModel = [self.mArr objectAtIndex:indexPath.row];
    
    FirstCell *firstCell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    firstCell.firstModel = self.firstModel;
    
    return firstCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreenHeight * 100 / 667;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    self.firstModel = [self.mArr objectAtIndex:indexPath.row];
    [center postNotificationName:@"webLink" object:self.firstModel];
}

/* 分割线隐藏 */
- (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

- (void)dataHandle
{
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:self.urlStr resultBlock:^(id result) {
        self.mDic = result;
        NSArray *arr = [self.mDic objectForKey:@"newsList"];
        for (NSDictionary *dic in arr) {
            self.firstModel = [[FirstModel alloc] init];
            [self.firstModel setValuesForKeysWithDictionary:dic];
        
            [self.mArr addObject:self.firstModel];
        }
        [self reloadData];
        [self.activity stopAnimating];
    }];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
